package com.chia.multienty.transaction.base.seata.at;

import io.seata.tm.api.GlobalTransaction;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Slf4j
public final class SeataTransactionHolder {

    private static final ThreadLocal<GlobalTransaction> CONTEXT = new ThreadLocal<>();

    /**
     * Set seata global transaction.
     *
     * @param transaction global transaction context
     */
    public static void set(final GlobalTransaction transaction) {
        CONTEXT.set(transaction);
    }

    /**
     * Get seata global transaction.
     *
     * @return global transaction
     */
    static GlobalTransaction get() {
        return CONTEXT.get();
    }

    /**
     * Clear global transaction.
     */
    static void clear() {
        log.debug("[SEATA] SEATA事务实例已销毁,堆栈信息如下: {}", Thread.currentThread().getStackTrace());
        CONTEXT.remove();
    }
}
